Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Switch

Switch examples

Here are some examples of the switch statement in Java:

Example: Get day in a week using switch statement

Get day in java //Copy and run the code and see the output, you would be able to understand the concept of switch public class Main{ public static void main(String args[]){ Day obj=new Day(); String result=obj.getDayOfWeek(6); System.out.println(result); } } public class Day{ public String getDayOfWeek(int day) { String dayName; switch (day) { case 1: dayName = "Sunday"; break; case 2: dayName = "Monday"; break; case 3: dayName = "Tuesday"; break; case 4: dayName = "Wednesday"; break; case 5: dayName = "Thursday"; break; case 6: dayName = "Friday"; break; case 7: dayName = "Saturday"; break; default: dayName = "Invalid day"; } return dayName; } }

Output

Friday
In this example, the getDayOfWeek method takes an integer day as input and uses a switch statement to determine the corresponding day of the week. It returns the day's name as a string.

Example: Calculate grade using Switch in java

Calculating grade in java //Copy and run the code and see the output, you would be able to understand the concept of switch //Copy and run the code and see the output, you would be able to understand the concept of switch public class Main{ public static void main(String args[]) { String grade; int score=88; int roundedScore = (int) Math.round(score); switch (roundedScore) { case 90: case 91: case 92: case 93: case 94: case 95: case 96: case 97: case 98: case 99: case 100: grade = "A+"; break; case 85: case 86: case 87: case 88: case 89: grade = "A"; break; case 80: case 81: case 82: case 83: case 84: grade = "A-"; break; case 75: case 76: case 77: case 78: case 79: grade = "B+"; break; case 70: case 71: case 72: case 73: case 74: grade = "B"; break; default: grade = "F"; } System.out.println(grade); } }

Output

A
In this example, the calculateGrade method takes a double score as input and calculates the corresponding letter grade based on a custom grading scale using a switch statement. It rounds the score to the nearest integer using Math.round and then assigns a letter grade based on specific score ranges.

Example : Simple grading by switch

Grading system using switch //Copy and run the code and see the output, you would be able to understand the concept of switch public class Main{ public static void main(String args[]){ char grade; int score=92; switch (score / 10) { case 9: case 10: grade = 'A'; break; case 8: grade = 'B'; break; case 7: grade = 'C'; break; case 6: grade = 'D'; break; default: grade = 'F'; } System.out.println(grade); } }

Output

A
The score is 85. When you divide 85 by 10, you get 8.5, but since score and 10 are both integers, integer division is performed, which results in 8. Therefore, the switch statement evaluates the case 8:. In this case, grade is assigned the value 'B' because the score falls into the range specified by the case 8:. So, the output is that grade is assigned the value 'B'.

  📌TAGS

★switch ★switch statement ★control statement ★control in java ★switch in java

Tutorials